<?php
	//session_start();

	// returns array searchResults, where each listing has the following
	// filename, username, UID, CID
	
	function Search()
	{
		$nextListing = -1;
		if (isset($_SESSION['lastListingID']))
			$nextListing = $_SESSION['lastListingID'];
			
		// get search query
		$searchQuery = $_SESSION['search'];
		
		// figure out which table are we searching from
		$searchType = "articles_table";		// assume article search
		if (trim($_SESSION['searchType']) == "Search Images")
			$searchType = "images_table";	// change if necessary
		
		// make query for SIMILAR searchQueries, and order by title, then popularity
		// page rank still not done
		
		if (($_SESSION['status'] == "Moderator") || ($_SESSION['status'] == "Member"))	// logged in, approved members get unrestricted search
		{
			echo "logged in search ";
			$query = "SELECT * FROM $searchType WHERE KW1 like '%$searchQuery%' OR KW2 like '%$searchQuery%' OR KW3 like '%$searchQuery%' OR KW4 like '%$searchQuery%' OR KW5 like '%$searchQuery%' OR KW6 like '%$searchQuery%' OR KW7 like '%$searchQuery%' OR KW8 like '%$searchQuery%' OR KW9 like '%$searchQuery%' OR \n"
				. "KW10 like '%$searchQuery%' OR KW11 like '%$searchQuery%' OR KW12 like '%$searchQuery%' OR KW13 like '%$searchQuery%' OR KW14 like '%$searchQuery%' OR \n"
				. "KW15 like '%$searchQuery%' OR KW16 like '%$searchQuery%' OR KW17 like '%$searchQuery%' OR KW18 like '%$searchQuery%' OR KW19 like '%$searchQuery%' OR \n"
				. "KW20 like '%$searchQuery%' or Filename like '%$searchQuery%' ORDER BY pageviews, Title";
		}
		elseif ($_SESSION['search'] != $_POST['search'])	// get fresh search listings
		{
			echo "not logged in, first search ";
			$query = "SELECT * FROM $searchType WHERE KW1 like '%$searchQuery%' OR KW2 like '%$searchQuery%' OR KW3 like '%$searchQuery%' OR KW4 like '%$searchQuery%' OR KW5 like '%$searchQuery%' OR KW6 like '%$searchQuery%' OR KW7 like '%$searchQuery%' OR KW8 like '%$searchQuery%' OR KW9 like '%$searchQuery%' OR \n"
				. "KW10 like '%$searchQuery%' OR KW11 like '%$searchQuery%' OR KW12 like '%$searchQuery%' OR KW13 like '%$searchQuery%' OR KW14 like '%$searchQuery%' OR \n"
				. "KW15 like '%$searchQuery%' OR KW16 like '%$searchQuery%' OR KW17 like '%$searchQuery%' OR KW18 like '%$searchQuery%' OR KW19 like '%$searchQuery%' OR \n"
				. "KW20 like '%$searchQuery%' or Filename like '%$searchQuery%' AND Private='no' ORDER BY pageviews, Title";
		}
		else	// continue with guest search listings
		{
			echo "not logged in, continued search ";
			$query = "SELECT * FROM $searchType WHERE KW1 like '%$searchQuery%' OR KW2 like '%$searchQuery%' OR KW3 like '%$searchQuery%' OR KW4 like '%$searchQuery%' OR KW5 like '%$searchQuery%' OR KW6 like '%$searchQuery%' OR KW7 like '%$searchQuery%' OR KW8 like '%$searchQuery%' OR KW9 like '%$searchQuery%' OR \n"
			. "KW10 like '%$searchQuery%' OR KW11 like '%$searchQuery%' OR KW12 like '%$searchQuery%' OR KW13 like '%$searchQuery%' OR KW14 like '%$searchQuery%' OR \n"
			. "KW15 like '%$searchQuery%' OR KW16 like '%$searchQuery%' OR KW17 like '%$searchQuery%' OR KW18 like '%$searchQuery%' OR KW19 like '%$searchQuery%' OR \n"
			. "KW20 like '%$searchQuery%' or Filename like '%$searchQuery%' AND Private='no' AND CID = $nextListing ORDER BY pageviews, Title";
		}

		$response = mysql_query($query);
		
		// for guest users, they have a limit on what they can see.
		$guestLimit = 0;
		$_SESSION['lastListingID'] = -1;

		if ($response)
		{
			while($results = mysql_fetch_array($response))
			{
				// if we reach the limit for guest, stop getting any more queries.
				if ($guestLimit == 3)
					break;
					
				if (!isset($_SESSION['user']))
					$guestLimit++;

				$filename = $results['Filename'];
				$UID = intval($results['UID']);
				$CID = intval($results['CID']);
				$lastMod = "";
				if($searchType == 'articles_table')
					$lastMod = $results['lastmodified'];
				
				$views = $results['pageviews'];
				
				// look for the username
				$entry = mysql_query("SELECT Username FROM user_table WHERE uid=$UID");						
				$entry = mysql_fetch_array($entry);
				$userName = $entry['Username'];
				
				// place all search results in an array
				$searchResults[] = array($filename, $userName, $UID, $CID, $lastMod, $views);
			}
			
			$_SESSION['lastListingID'] = intval($results['CID']);
			if ($guestLimit < 3)
				$_SESSION['lastListingID'] = -1;
			echo $guestLimit . ' ';
			echo $_SESSION['lastListingID'];
		}
		return $searchResults;
	}
?>